package com.itboye.zhujia;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.zhujia.base.BaseFragment;
import com.itboye.zhujia.constants.SPContants;
import com.itboye.zhujia.controller.LoginController;
import com.itboye.zhujia.custom.RoundImageView;
import com.itboye.zhujia.login.LoginActivity;
import com.itboye.zhujia.me.ActivityMyData;
import com.itboye.zhujia.me.ActivitySetting;
import com.itboye.zhujia.me.key.ActivityAdministrationKey;
import com.itboye.zhujia.util.SPUtils;
import com.itboye.zhujia.util.XImageLoader;

/**
 * 我的
 * 
 * @author
 * 
 */
public class MeFragment extends BaseFragment implements OnClickListener {
	ImageView close_icon, img_setting;
	TextView add_shap_title_tv;
	RoundImageView roundImage;
	TextView name;
	ImageView queryPersonData;
	TextView phone_num;
	LinearLayout lin_mywallet;// 钱包
	LinearLayout lin_mycollection;// 收藏
	LinearLayout lin_myappointment;// 预约
	LinearLayout lin_mysign;// 签约
	RelativeLayout adddress_manage;// 确认房源
	TextView address_tv;// 房源地址
	RelativeLayout mylifemanager_rel;// 生活管家
	RelativeLayout myintelligent_lock_rel;// 智能锁
	RelativeLayout myassess_rel;// 评价
	RelativeLayout invitefriend_rel;// 邀请好友
	RelativeLayout help_rel;// 帮助中心
	LinearLayout head;// 头像（整块）
	TextView geren_tv, jingjiren_tv;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_person_centre;
	}

	@Override
	public void onMyActivityCreated() {
		// TODO Auto-generated method stub
		close_icon.setVisibility(View.GONE);
		img_setting.setVisibility(View.VISIBLE);
		add_shap_title_tv.setText("个人中心");
		// close_icon.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// // finish();
		// }
		// });
		myintelligent_lock_rel.setOnClickListener(this);

	}
	@Override
	public void onStart() {
		super.onStart();
		read();
	}
	
	/**
	 * 读取数据
	 */
	private void read(){
		String resultData = (String) SPUtils.get(getActivity(), null,
				SPContants.AVATAR_URL, "");
		XImageLoader.load(resultData, roundImage);
		
		initMobile();

		// close_icon.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// // finish();
		// }
		// });
		myintelligent_lock_rel.setOnClickListener(this);


	}
	/**
	 * 加入数据
	 */
	private void initMobile() {
		String phonenumber = (String) SPUtils.get(getActivity(), null,
				SPContants.NICK, "");
		String nsername = (String) SPUtils.get(getActivity(), null,
				SPContants.USERNAME, "");
		name.setText("" + phonenumber);

		phone_num.setText(nsername);
	}
	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.head:
			LoginController.onMyPersonalCenter(getActivity(), null);
			break;
		case R.id.lin_mywallet:// 钱包
			LoginController.onMyMoneyClick(getActivity(), null);
			break;
		case R.id.lin_mycollection:// 收藏
			LoginController.onMyCollectionClick(getActivity(), null);
			break;
		case R.id.lin_myappointment:// 预约
			LoginController.onMyAppointmentClick(getActivity(), null);
			break;
		case R.id.lin_mysign:// 签约
			LoginController.onMySignClick(getActivity(), null);
			break;
		case R.id.adddress_manage:// 确认房源
			intent = new Intent();
			startActivity(intent);
			break;
		case R.id.mylifemanager_rel:// 生活管家
			intent = new Intent();
			startActivity(intent);
			break;
		case R.id.myintelligent_lock_rel:// 智能锁
			intent = new Intent(getActivity(), ActivityAdministrationKey.class);
			startActivity(intent);
			break;
		case R.id.myassess_rel:// 评价
			intent = new Intent();
			startActivity(intent);
			break;
		case R.id.invitefriend_rel:// 邀请好友
			intent = new Intent();
			startActivity(intent);
			break;
		case R.id.help_rel:// 帮助中心（跳转到登录  测试）
			intent = new Intent(getActivity(), LoginActivity.class);
			startActivity(intent);
			break;

		case R.id.img_setting:
			intent = new Intent(getActivity(), ActivitySetting.class);
			startActivity(intent);
			break;
		case R.id.geren_tv:
			geren_tv.setAlpha(0.8f);
			jingjiren_tv.setAlpha(0.4f);
			break;
		case R.id.jingjiren_tv:
			geren_tv.setAlpha(0.4f);
			jingjiren_tv.setAlpha(0.8f);
			break;
		case R.id.queryPersonData:
			intent = new Intent(getActivity(), ActivityMyData.class);
			startActivity(intent);
		default:
			break;
		}
	}
	/**
	 * 广播   当收到头像更改    更换头像
	 */
	BroadcastReceiver avartBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			String resultData = (String) SPUtils.get(getActivity(), null,
					SPContants.AVATAR_URL, "");

			initMobile();
			XImageLoader.load(resultData, roundImage);
		};
	};
}
